package com.ece.friendly.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ece.friendly.jdbcConnection.JdbcConnection;
import com.ece.friendly.model.Article;

@Service
public class ArticleDaoImp implements ArticleDao{

	@Autowired
	private JdbcConnection jdbcConnection;
	
	@Override
	public List<Article> getAllArticle() {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<Article> articles=new ArrayList<Article>();
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Article order by date_post desc;");
			Article art=null;
			while(resultat.next()){
				art=new Article(Integer.parseInt(resultat.getString(1)), resultat.getString(2), resultat.getString(3), Integer.parseInt(resultat.getString(4)),resultat.getString(5),resultat.getString(6),Integer.parseInt(resultat.getString(7)),Float.parseFloat(resultat.getString(8)),dateFormat.parse(resultat.getString(9)),resultat.getString(10));
				articles.add(art);
			}
			return articles;
		} catch (SQLException | NumberFormatException | ParseException e) {
			e.printStackTrace();return null;
		}
	}
	@Override
	public Article getArticle(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<Article> getArticles(String login) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<Article> articles=new ArrayList<Article>();
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Article where stylie='"+login+"' order by date_post desc;");
			Article art=null;
			while(resultat.next()){
				art=new Article(Integer.parseInt(resultat.getString(1)),  resultat.getString(2),resultat.getString(3), Integer.parseInt(resultat.getString(4)),resultat.getString(5),resultat.getString(6),Integer.parseInt(resultat.getString(7)),Float.parseFloat(resultat.getString(8)),dateFormat.parse(resultat.getString(9)),resultat.getString(10));
				articles.add(art);
			}
			return articles;
		} catch (SQLException | NumberFormatException | ParseException e) {
			e.printStackTrace();return null;
		}
		
	}
	@Override
	public List<Article> getTopArticles(int limit) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<Article> articles=new ArrayList<Article>();
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Article order by `like` desc limit "+limit+";");
			Article art=null;
			while(resultat.next()){
				art=new Article(Integer.parseInt(resultat.getString(1)), resultat.getString(2), resultat.getString(3), Integer.parseInt(resultat.getString(4)),resultat.getString(5),resultat.getString(6),Integer.parseInt(resultat.getString(7)),Float.parseFloat(resultat.getString(8)),dateFormat.parse(resultat.getString(9)),resultat.getString(10));
				articles.add(art);
			}
			return articles;
		} catch (SQLException | NumberFormatException | ParseException e) {
			e.printStackTrace();return null;
		}
		
	}
	@Override
	public boolean addArticle(String login, String type_event, String nom,int etat, String conseil, float prix_jour, String photo,long date) 
	{
		
		try {
			if(jdbcConnection.getStat().executeUpdate("insert into article values (NULL,'"+login+"','"+type_event+"','0','"+nom+"','"+conseil+"','"+etat+"','"+prix_jour+"','"+new Timestamp(date)+"','"+photo+"')")>0)
			{
				jdbcConnection.getConnection().commit();
				return true;
			}
			else return false;
        
		}
		catch (SQLException e1) {
			return false;
		}
	}
	@Override
	public Article getArticle(String stylie, long date) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ResultSet resultat = jdbcConnection.getStat().executeQuery("SELECT * FROM Article where stylie='"+stylie+"' and date_post='"+new Timestamp(date)+"';");
			Article article=null;
			while(resultat.next()){
				article=new Article(Integer.parseInt(resultat.getString(1)), resultat.getString(2), resultat.getString(3), Integer.parseInt(resultat.getString(4)),resultat.getString(5),resultat.getString(6),Integer.parseInt(resultat.getString(7)),Float.parseFloat(resultat.getString(8)),dateFormat.parse(resultat.getString(9)),resultat.getString(10));
		}
			return article;
		} catch (SQLException | NumberFormatException | ParseException e) {
			e.printStackTrace();return null;
		}
	}
	

}
